package forum.dao;

import forum.model.Section;
import forum.util.DBUtil;
import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author haomin
 * @date 2022/06/30 11:32
 **/
public class SectionDao {

    // 得到板块集合ByUid
    @SneakyThrows
    public List<Section> selectListByUid(){
        List<Section> list = new ArrayList<>();
        String sql = "select sID, uID, sName, sStatement, sClickCount, sTopicCount from section;";
        try (Connection c = DBUtil.connection()) {
            try (PreparedStatement ps = c.prepareStatement(sql)){
                System.out.println("DEBUG: " + ps);
                try (ResultSet rs = ps.executeQuery()){
                    while (rs.next()){
                        Section section = new Section();
                        section.sID = rs.getInt("sID");
                        section.uID = rs.getInt("uID");
                        section.sName = rs.getString("sName");
                        section.sStatement = rs.getString("sStatement");
                        section.sClickCount = rs.getInt("sClickCount");
                        section.sTopicCount = new TopicDao().sTopicCount(section.sID);

                        list.add(section);
                    }
                }
            }
        }
        return list;
    }

    // 得到板块集合ByKeyWord
    @SneakyThrows
    public List<Section> selectSectionListByKeyword(String keyword) {
        List<Section> list = new ArrayList<>();
        String sql = "select * from section where sName like ?;";

        System.out.println(sql);
        try (Connection c = DBUtil.connection()) {
            System.out.println(sql);

            try (PreparedStatement ps = c.prepareStatement(sql)){
                System.out.println(sql);
                String key = "%"+keyword+"%";
                ps.setString(1,key);
                System.out.println(sql);

                try (ResultSet rs = ps.executeQuery()){
                    while (rs.next()){
                        Section section = new Section();
                        section.sID = rs.getInt("sID");
                        section.uID = rs.getInt("uID");
                        section.sName = rs.getString("sName");
                        section.sStatement = rs.getString("sStatement");
                        section.sClickCount = rs.getInt("sClickCount");
                        section.sTopicCount = new TopicDao().sTopicCount(section.sID);

                        list.add(section);
                    }
                }
            }
        }
        return list;
    }

    // 更新浏览次数
    @SneakyThrows
    public void update(int sid) {
        String sql = "update section set sClickCount = sClickCount+1 where sid = ?";
        try (Connection c = DBUtil.connection()) {
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setInt(1,sid);

                ps.executeUpdate();
            }
        }
    }
}